﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using yq.ControllerBase;
using yq.Dictionary;

namespace yq.Controller.Home
{
    /// <summary>
    /// 默认页
    /// </summary>
    public class HomeController : BaseController
    {
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="test"></param>
        /// <returns></returns>
        public ActionResult Index(string test)
        {
        
            return View();
        }

        [ValidateInput(false)]
        public ActionResult List(yq.Model.Logic.UserAccount model)
        {
            //model.UserId;

            //var r= yq.GetService.Create<yq.IService.IUpload>().UpTxt("sdfsdfsdfsdfsdf","222");
            //var d= yq.GetService.Create<yq.IService.IUpload>().DownFileTxt(r.Value);

            return View(new yq.Model.Logic.UserAccount() { CreateTtime = DateTime.Now, UserId = "83bcc673d3f949b08b4955a9a68740e9" });
        }

        [VerifyFilter("Id", VerifyHelp.TestKey)]
        public ActionResult Test()
        {
            string test1 = yq.ControllerBase.LanguageUser.LanguageString("测试");
            yq.ControllerBase.LanguageUser.SetLanguage(Language.ConfigEnum.EN);

            string test2 = yq.ControllerBase.LanguageUser.LanguageString("测试");
            return View();
        }



        /// <summary>
        /// 初始化管理员
        /// </summary>
        /// <returns></returns>
        [NoPurviewFilter()]
        public ActionResult Initialize()
        {
            var obj = new yq.WebModel.InitializeAdmin();

            return View(obj);
        }

        /// <summary>
        /// 初始化管理员 提交
        /// </summary>
        /// <returns></returns>
        [NoPurviewFilter()]
        public ActionResult DoInitialize()
        {
            yq.WebModel.InitializeAdmin info = new WebModel.InitializeAdmin();
            TryUpdateModel<yq.WebModel.InitializeAdmin>(info);
            var rv = yq.GetService.Create<yq.IService.IUser>().RegisterAdmin(info.Login, info.Account);

            return Json(rv);
        }

        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        [NoPurviewFilter()]
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 执行登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [NoPurviewFilter()]
        [ErrorFilter("登录失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoLogin(string account = "", string pwd = "")
        {
            ReturnValue<string> rv = new ReturnValue<string>();

            if (account.Length == 0 || pwd.Length == 0)
            {
                rv.False("请输入账号和密码");
            }
            else
            {
                //根据登录账号和密码查询用户Id
                rv = yq.GetService.Create<yq.IService.IUser>().Login(account, pwd);

                if (rv.Status == ReturnEnum.Yes)
                {
                    //创建session
                    rv = new yq.ControllerBase.CurrentUser().Login(rv.Value);
                }
            }

            return Json(rv);
        }

    }
}
